package com.kun.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.kun.entity.CaseEntity;
import com.kun.server.CarServer;
import com.kun.server.LoginServer;
import com.kun.server.ReportServer;
import com.kun.server.impl.CarServerImpl;
import com.kun.server.impl.LoginServerImpl;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 报案
 * @author Administrator
 *
 */
@Controller
@Namespace("/")
public class ReportAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private CaseEntity caseEntity;
	@Resource(name="ReportServerImpl")
	private ReportServer reportServer;
	
	@Resource(name="LoginServerImpl")
	private LoginServer ls;
	
	@Resource(name="CarServerImpl")
	private CarServer carServer;
	
	private List<File> fileN;//上传的文件
	private List<String> fileNContentType;//文件类型
	private List<String> fileNFileName;//上传的文件名
	private String savePath;//保存地址
	
	/**
	 * 添加报案
	 * @return
	 */
	@Action(value="top_addCase",
			results={
				@Result(name="success",location="/index.jsp"),
				@Result(name="error",location="/error.jsp"),
				@Result(name="input",location="/error.jsp")},
			interceptorRefs={
				@InterceptorRef(value="fileUpload",params={
					"allowedTypes","image/jpeg,image/gif",
					"maximumSize","1024*1024"}),
				@InterceptorRef("defaultStack")},
			params={
				"savePath","top/uploadFile"
	})
	public String addCase(){
		int customerid = (Integer)ServletActionContext.getRequest()
					.getSession().getAttribute("customerid");
		try {
			String upload = upload();
			caseEntity.setCaseImgPath(upload);
		} catch (Exception e) {
			e.printStackTrace();
		}
		caseEntity.setCustomerid(customerid);
		reportServer.addCase(caseEntity);
		return SUCCESS;
	}
	
	/**here
	 * 查询所有的案件(about me)
	 * @return
	 */
	public String findAllCase(){
		int customerid = (Integer)ServletActionContext.getRequest()
					.getSession().getAttribute("customerid");
		List<CaseEntity> caseinfo = reportServer.findAllCase(customerid);
		ServletActionContext.getRequest().setAttribute("caseinfo",caseinfo);
		return SUCCESS;
	}
	/**
	 * 在报案前查询各种信息(用户信息,车辆信息,接案员信息,报案类型)
	 */
	public String findMessageBeforeReport(){
		//从session中获取客户id
		int customerid = (Integer)ServletActionContext.getRequest()
					.getSession().getAttribute("customerid");
		//获取电话号码
		String phone = ls.selPhoneById(customerid);
		//获取该用户的所有车牌
		List<String> carIdList = carServer.selCarIdById(customerid);
		//... 接案员信息,报案类型
		ServletActionContext.getRequest().setAttribute("phone",phone);
		ServletActionContext.getRequest().setAttribute("carIdList",carIdList);
		
		return SUCCESS;
	}
	
	/**
	 * 上传图片
	 * @return
	 * @throws Exception
	 */
	public String upload() throws Exception{
		File file = new File(getSavePath());
		if(!file.exists()){
			file.mkdir();
		}
		String st = "";
		try {
			for(int i =0;i<fileN.size();i++){
				//获取后缀名 <.jpg>
				String str = getFileNFileName().get(i).substring(getFileNFileName().get(i).lastIndexOf("."));
				String beginName = Long.toString(System.currentTimeMillis());
				Thread.sleep(200);
				String name = beginName+str;
				//文件输入流    把文件读过来
				FileInputStream fis;
				try {
					fis = new FileInputStream(getFileN().get(i));
					//文件输出流 把文件保存到自己定义的目录下
					FileOutputStream fos = new FileOutputStream(file+File.separator+name);
					byte[] by = new byte[fis.available()];
					int length = 0;
					while((length = fis.read(by)) > 0){
						fos.write(by,0,length);
					}
					st = st + (savePath+File.separator+name)+",";
					fos.close();
					fis.close();
				} catch (FileNotFoundException e) {
					throw new FileNotFoundException("文件找不到或者出错了！");
				} catch (IOException e) {
					throw new java.io.IOException("IO流报的错误");
				}
			}	
		} catch (NullPointerException e) {
			throw new Exception("...没有选择文件");
		}
		String paths = st.substring(0,st.length()-1);
		return paths;
	}
	
	//getter setter
	public CaseEntity getCaseEntity() {
		return caseEntity;
	}
	public void setCaseEntity(CaseEntity caseEntity) {
		this.caseEntity = caseEntity;
	}
	public List<File> getFileN() {
		return fileN;
	}
	public void setFileN(List<File> fileN) {
		this.fileN = fileN;
	}
	public List<String> getFileNContentType() {
		return fileNContentType;
	}
	public void setFileNContentType(List<String> fileNContentType) {
		this.fileNContentType = fileNContentType;
	}
	public List<String> getFileNFileName() {
		return fileNFileName;
	}
	public void setFileNFileName(List<String> fileNFileName) {
		this.fileNFileName = fileNFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
}
